LiteLFG = CreateFrame"Frame"
LiteLFG:SetScript("OnEvent", function(self, event, ...) self[event](self, event, ...) end)

local L = LITELFG_LOCALE
local format = string.format

BINDING_HEADER_LITELFG_LITELFG = "LiteLFG"
BINDING_NAME_LITELFG_TOGGLELFG = L.LITELFG_TOGGLELFG

--Create Frames
local handleLFGWizardFrame = CreateFrame("Button", nil, LFGWizardFrame, "UIPanelButtonTemplate");
handleLFGWizardFrame:SetWidth(110);
handleLFGWizardFrame:SetHeight(22);
handleLFGWizardFrame:SetPoint("TOPRIGHT", "LFGWizardFrame", "TOPRIGHT", -50, -45);
handleLFGWizardFrame:SetScript("OnClick", function() LiteLFG.ToggleLFGChannel(); HideUIPanel(LFGParentFrame); end);

local handleLFGFrame = CreateFrame("Button", nil, LFGFrame, "UIPanelButtonTemplate");
handleLFGFrame:SetWidth(110);
handleLFGFrame:SetHeight(22);
handleLFGFrame:SetPoint("TOPRIGHT", "LFGFrame", "TOPRIGHT", -50, -45);
handleLFGFrame:SetScript("OnClick", function() LiteLFG.ToggleLFGChannel(); HideUIPanel(LFGParentFrame); end);

local handleLFMFrame = CreateFrame("Button", nil, LFMFrame, "UIPanelButtonTemplate");
handleLFMFrame:SetWidth(110);
handleLFMFrame:SetHeight(22);
handleLFMFrame:SetPoint("TOPRIGHT", "LFMFrame", "TOPRIGHT", -50, -45);
handleLFMFrame:SetScript("OnClick", function() LiteLFG.ToggleLFGChannel(); HideUIPanel(LFGParentFrame); end);

local print = function (msg)
    DEFAULT_CHAT_FRAME:AddMessage(tostring(msg or "nil"));
end

local GetTalentString = function ()
    local name1, _, pointsSpent1, _ = GetTalentTabInfo(1);
    local name2, _, pointsSpent2, _ = GetTalentTabInfo(2);
    local name3, _, pointsSpent3, _ = GetTalentTabInfo(3);
    local nameKey = L.NONE;
    if( not(pointsSpent1 == 0 and pointsSpent2 == 0 and pointsSpent3 == 0) ) then
        local tmpname, temppointsSpent;
        tmpname = ((pointsSpent1 > pointsSpent2) and name1) or name2;
        temppointsSpent = ((pointsSpent1 > pointsSpent2) and pointsSpent1) or pointsSpent2;
        tmpname = ((temppointsSpent > pointsSpent3) and tmpname) or name3;
        nameKey = tmpname;
    end
    return ("%s (%s/%s/%s)"):format(nameKey, pointsSpent1, pointsSpent2, pointsSpent3);
end


function LiteLFG:SetTextToButton()
    local _, _, _, _, _, _, _, _, _, _, lfgStatus, lfmStatus, _ = GetLookingForGroup();
    if (lfgStatus or lfmStatus) then
        handleLFGWizardFrame:SetText(L.LEAVEBUTTON);
        handleLFGFrame:SetText(L.LEAVEBUTTON);
        handleLFMFrame:SetText(L.LEAVEBUTTON);
    else
        handleLFGWizardFrame:SetText(L.JOINBUTTON);
        handleLFGFrame:SetText(L.JOINBUTTON);
        handleLFMFrame:SetText(L.JOINBUTTON);
    end
end

function LiteLFG:ToggleLFGChannel()
    local _, _, _, _, _, _, _, _, _, _, lfgStatus, lfmStatus, _ = GetLookingForGroup();
    if (lfgStatus) then
        ClearLookingForGroup();
        return;
    end
    if (lfmStatus) then
        ClearLookingForMore();
        return;
    end

    -- while single
    if ((GetRealNumPartyMembers() == 0) and (GetRealNumRaidMembers() == 0)) then
        ClearLookingForGroup();
        SetLookingForGroup(3,5,1);
        ClearLFGAutojoin();
    -- while in group
    elseif ((GetRealNumPartyMembers() > 0) and (GetRealNumRaidMembers() == 0) and (IsPartyLeader("player"))) then
        ClearLookingForMore();
        SetLookingForMore(5,1);
        ClearLFMAutofill();
    -- while in raid
    elseif ((GetRealNumRaidMembers() > 0) and (IsRaidOfficer() or IsRaidLeader())) then
        ClearLookingForMore();
        SetLookingForMore(5,1);
        ClearLFMAutofill();
    end
    lfgComment = GetTalentString() .. " - Powered by LiteLFG";
    SetLFGComment(lfgComment);
end

LiteLFG:RegisterEvent("VARIABLES_LOADED");
LiteLFG:RegisterEvent("PARTY_MEMBERS_CHANGED");
LiteLFG:RegisterEvent("CHAT_MSG_CHANNEL_NOTICE");
-- a crash will be occured in using LFG_UPDATE event
-- LiteLFG:RegisterEvent("LFG_UPDATE");


function LiteLFG:VARIABLES_LOADED(self, event, ...)
	LiteLFG.SetTextToButton();
	SLASH_llfg1 = "/llfg";
	SlashCmdList["llfg"] = LiteLFG.ToggleLFGChannel;
	LiteLFG:UnregisterEvent("VARIABLES_LOADED");
	LiteLFG.VARIABLES_LOADED = nil;
end

function LiteLFG:PARTY_MEMBERS_CHANGED()
	LiteLFG.SetTextToButton();
end

function LiteLFG:CHAT_MSG_CHANNEL_NOTICE()
	LiteLFG.SetTextToButton();
end

